﻿using AccustomeAttributedll;
using AixCommInfo;
using LanCustomControldll.Unit_LineAix;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FinalTestMachine.Motion;


namespace FinalTestMachine.View.Dispenser
{
    /// <summary>
    /// Wnd_TrailSettings.xaml 的交互逻辑
    /// </summary>
    public partial class Wnd_TrailSettings : Window
    {
        public Wnd_TrailSettings()
        {
            InitializeComponent();
        }
        bool IsSetted = false;
        bool IsExit = false;
        RM_dll2.MotionHelper motion => StaticInitial.Motion;
        void ShowUI()
        {
            //try
            //{
            //    while (true)
            //    {
            //        if (IsExit) return;

            //        this.Dispatcher.Invoke(()=>                     
            //        {
            //            if(motion.CurOrder.IsAlarm||motion.CurOrder.IsResing||motion.CurOrder.IsAutoRunning || ThreeAixsFuncHepler.Fun_AnyProcessIsBusy())
            //            {
            //                funAixsButtonViewModel.Button_EnableOFF();

            //            }
            //            else
            //            {
            //                funAixsButtonViewModel.Button_EnableON();
            //            }




            //        });
            //        Thread.Sleep(100);
            //    }
            //}
            //catch (Exception ex)
            //{

            //    MessageBox.Show(ex.Message);
            //}
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            IsExit = false;
            //Thread thread = new Thread(ShowUI);
            //thread.IsBackground = true;
            //thread.Start();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            IsExit = true;
        }

        //public void SetComponent(ThreeAixsFuncHepler model)
        //{


        //    if (IsSetted) return;
        //    IsSetted = true;
        //    //=========================================================
        //    Type type = model.GetType();

        //    var memberInfos = type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(s => s.IsDefined(typeof(TextBoxRemarkAttribute), true) == true || s.IsDefined(typeof(FunctionSelectionRemarkAttribute), true) == true);

        //    //开始分组判断：1.默认分组，2.标识分组
        //    var NoGroupMembers = memberInfos.Where(s => s.IsDefined(typeof(GroupRemarkAttribute), true) == false);

        //    var RemarkGroupMembers = memberInfos.Where(s => s.IsDefined(typeof(GroupRemarkAttribute), true) == true);
        //    TabControl table = new TabControl();
        //    List<MemberInfo> funListLinePos = new List<MemberInfo>();//XY点信息
        //    List<MemberInfo> funListFun = new List<MemberInfo>();
         
        //    void AddTextBoxToGrid(Grid grid, IEnumerable<MemberInfo> enumerator)
        //    {
        //        grid.Margin = new Thickness(20, 20, 20, 20);
        //        int rowIndex = 0;
        //        if (grid.ColumnDefinitions.Count < 1)
        //        {
        //            int max = 2 - grid.ColumnDefinitions.Count;
        //            for (int i = 0; i < max; i++)
        //            {
        //                ColumnDefinition columnDefinition = new ColumnDefinition();
        //                columnDefinition.Width = GridLength.Auto;
        //                grid.ColumnDefinitions.Add(columnDefinition);
        //            }
        //        }
        //        funListLinePos.Clear();
        //        funListFun.Clear();
        //        foreach (var vr in enumerator)
        //        {


        //            if (vr.MemberType == MemberTypes.Field)
        //            {
        //                FieldInfo fieldInfo = vr as FieldInfo;
        //                object singleMsgSource = fieldInfo.GetValue(model);
        //                if (fieldInfo.FieldType.Name == typeof(ParamStructText<>).Name || fieldInfo.FieldType.Name == typeof(ParamObjectText<>).Name)
        //                {



        //                    //获取位置信息标识
        //                    TextBoxRemarkAttribute paramRemarkAttribute = (TextBoxRemarkAttribute)vr.GetCustomAttributes(typeof(TextBoxRemarkAttribute), true)[0];
        //                    //添加行
        //                    RowDefinition rowDefinition = new RowDefinition();
        //                    rowDefinition.Height = GridLength.Auto;
        //                    grid.RowDefinitions.Add(rowDefinition);

        //                    rowIndex = grid.RowDefinitions.IndexOf(rowDefinition);
        //                    TextBlock textBlock = new TextBlock();
        //                    textBlock.Text =LanguageControlBase.LanguageSearchHelper.GetLanguageExplain(paramRemarkAttribute.GetRemark());
        //                    if (!textBlock.Text.Contains(':'))
        //                    {
        //                        textBlock.Text = textBlock.Text + "：";
        //                    }
        //                    textBlock.FontSize = 15;
        //                    grid.Children.Add(textBlock);
        //                    textBlock.HorizontalAlignment = HorizontalAlignment.Right;
        //                    textBlock.VerticalAlignment = VerticalAlignment.Center;
        //                    Grid.SetRow(textBlock, rowIndex);
        //                    Grid.SetColumn(textBlock, 0);
        //                    //添加输入框
        //                    TextBox textBox = new TextBox();
        //                    textBox.Height = 25;
        //                    textBox.Width = 100;
        //                    textBox.FontSize = 15;
        //                    textBox.Margin = new Thickness(0, 5, 5, 5);
        //                    textBox.VerticalContentAlignment = VerticalAlignment.Bottom;
        //                    grid.Children.Add(textBox);
        //                    Grid.SetRow(textBox, rowIndex);
        //                    Grid.SetColumn(textBox, 1);
        //                    //绑定
        //                    Binding binding = new Binding();
        //                    binding.Source = singleMsgSource;
        //                    binding.Path = new PropertyPath("GetValue");
        //                    binding.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
        //                    textBox.SetBinding(TextBox.TextProperty, binding);

        //                }
        //                else if (singleMsgSource.GetType().Name == typeof(TwoAixsPosMsg).Name || singleMsgSource.GetType().Name == typeof(ThreeAixsPosMsg).Name)
        //                {
        //                    funListLinePos.Add(vr);
        //                }
        //                else if (fieldInfo.FieldType.Name == typeof(bool).Name)
        //                {
        //                    funListFun.Add(vr);
        //                }


        //            }


        //        }
        //    }


        //    void AddFunSetToGrid(Grid grid, IEnumerable<MemberInfo> enumerator)
        //    {

        //        grid.Margin = new Thickness(20, 20, 20, 20);
        //        int rowIndex = 0;
        //        if (grid.ColumnDefinitions.Count < 1)
        //        {
        //            int max = 2 - grid.ColumnDefinitions.Count;
        //            for (int i = 0; i < max; i++)
        //            {
        //                ColumnDefinition columnDefinition = new ColumnDefinition();
        //                columnDefinition.Width = GridLength.Auto;
        //                grid.ColumnDefinitions.Add(columnDefinition);
        //            }
        //        }
        //        foreach (var vr in enumerator)
        //        {
        //            if (vr.MemberType == MemberTypes.Field)
        //            {
        //                FieldInfo fieldInfo = vr as FieldInfo;
        //                if (fieldInfo.FieldType.Name == typeof(bool).Name)
        //                {


        //                    object singleMsgSource = fieldInfo.GetValue(model);
        //                    //获取位置信息标识
        //                    FunctionSelectionRemarkAttribute paramRemarkAttribute = (FunctionSelectionRemarkAttribute)vr.GetCustomAttributes(typeof(FunctionSelectionRemarkAttribute), true)[0];
        //                    //添加行
        //                    RowDefinition rowDefinition = new RowDefinition();
        //                    rowDefinition.Height = GridLength.Auto;
        //                    grid.RowDefinitions.Add(rowDefinition);

        //                    rowIndex = grid.RowDefinitions.IndexOf(rowDefinition);
        //                    TextBlock textBlock = new TextBlock();
        //                    textBlock.Text = LanguageControlBase.LanguageSearchHelper.GetLanguageExplain(paramRemarkAttribute.GetRemark());
        //                    if (!textBlock.Text.Contains(':'))
        //                    {
        //                        textBlock.Text = textBlock.Text + "：";
        //                    }
        //                    textBlock.FontSize = 15;
        //                    grid.Children.Add(textBlock);
        //                    textBlock.HorizontalAlignment = HorizontalAlignment.Right;
        //                    textBlock.VerticalAlignment = VerticalAlignment.Center;
        //                    Grid.SetRow(textBlock, rowIndex);
        //                    Grid.SetColumn(textBlock, 0);

        //                    //添加设置控件
        //                    Control control = paramRemarkAttribute.GetControl();

        //                    control.Height = 25;
        //                    control.Width = 100;
        //                    control.FontSize = 15;
        //                    control.Margin = new Thickness(0, 5, 5, 5);
        //                    control.VerticalContentAlignment = VerticalAlignment.Bottom;
        //                    grid.Children.Add(control);
        //                    Grid.SetRow(control, rowIndex);
        //                    Grid.SetColumn(control, 1);
        //                    //初始化控件
        //                    paramRemarkAttribute.SetControlIntial(bool.Parse(singleMsgSource.ToString()));


        //                    //设置事件
        //                    paramRemarkAttribute.actionUseTrigger += () =>
        //                    {
        //                        Type tt = (fieldInfo.GetValue(model)).GetType();

        //                        fieldInfo.SetValue(model, Convert.ChangeType(true, tt));

        //                    };

        //                    paramRemarkAttribute.actionLimitUseTrigger += () =>
        //                    {
        //                        Type tt = (fieldInfo.GetValue(model)).GetType();

        //                        fieldInfo.SetValue(model, Convert.ChangeType(false, tt));


        //                    };

        //                }

        //            }

        //        }
        //    }

        //    //判断默认分组里面是否含有元素，如果有则创建默认分组页

        //    if (NoGroupMembers.Count() > 0)
        //    {
        //        //创建默认分组页
        //        TabItem tabItemNoGroup = new TabItem();
        //        tabItemNoGroup.Header = "默认";
        //        tabItemNoGroup.FontSize = 15;
        //        tabItemNoGroup.MinHeight = 20;
        //        //创建的容器
        //        Grid wrapPanel = new Grid();
        //        AddTextBoxToGrid(wrapPanel, NoGroupMembers);
        //        if (wrapPanel.Children.Count > 0)
        //        {
        //            tabItemNoGroup.Content = wrapPanel;
        //            table.Items.Add(tabItemNoGroup);
        //        }
        //        //创建XYZ位置信息
        //        if (funListLinePos.Count > 0)
        //        {
        //            TabItem tabItem_XYZ = new TabItem();
        //            tabItem_XYZ.Header = "默认XYZ";
        //            tabItem_XYZ.FontSize = 15;
        //            tabItem_XYZ.MinHeight = 20;
        //            UC_LinePosInfo unit_LinePosMsgViewmodel = new UC_LinePosInfo();
        //            unit_LinePosMsgViewmodel.SetLinePosInfo(funListLinePos, model);
        //            tabItem_XYZ.Content = unit_LinePosMsgViewmodel;
        //            table.Items.Add(tabItem_XYZ);

        //        }
        //        funListLinePos.Clear();
        //        //创建功能
        //        if (funListFun.Count > 0)
        //        {
        //            TabItem tabItem_Fun = new TabItem();
        //            tabItem_Fun.Header = "默认功能";
        //            tabItem_Fun.FontSize = 15;
        //            tabItem_Fun.MinHeight = 20;
        //            //创建的容器
        //            Grid wrapPanelFun = new Grid();
        //            AddFunSetToGrid(wrapPanelFun, funListFun);
        //            tabItem_Fun.Content = wrapPanelFun;


        //            table.Items.Add(tabItem_Fun);
        //        }
        //        funListFun.Clear();


        //    }
        //    if (RemarkGroupMembers.Count() > 0)
        //    {
        //        //创建标识分组
        //        Dictionary<string, List<MemberInfo>> keyValuePairs = new Dictionary<string, List<MemberInfo>>();
        //        foreach (var vr in RemarkGroupMembers)
        //        {
        //            MemberInfo methodInfo = vr as MemberInfo;
        //            //获取分组信息标识
        //            GroupRemarkAttribute paramRemarkAttribute = (GroupRemarkAttribute)vr.GetCustomAttributes(typeof(GroupRemarkAttribute), true)[0];
        //            if (keyValuePairs.Keys.Contains(paramRemarkAttribute.GetRemark()))
        //            {
        //                keyValuePairs[paramRemarkAttribute.GetRemark()].Add(vr);
        //            }
        //            else
        //            {
        //                keyValuePairs.Add(paramRemarkAttribute.GetRemark(), new List<MemberInfo>());
        //                keyValuePairs[paramRemarkAttribute.GetRemark()].Add(vr);
        //            }

        //        }

        //        foreach (var vr in keyValuePairs.Keys)
        //        {
        //            var temp = keyValuePairs[vr].FirstOrDefault();
        //            if (temp.MemberType == MemberTypes.Field)
        //            {
        //                FieldInfo fieldInfo = temp as FieldInfo;
        //                object singleMsgSource = fieldInfo.GetValue(model);
        //                if (fieldInfo.FieldType.Name == typeof(ParamStructText<>).Name || fieldInfo.FieldType.Name == typeof(ParamObjectText<>).Name)
        //                {
        //                    TabItem tabItem = new TabItem();
        //                    tabItem.Header = vr;
        //                    tabItem.FontSize = 15;
        //                    tabItem.MinHeight = 20;
        //                    //创建按键的容器
        //                    Grid wrapPanel = new Grid();
        //                    AddTextBoxToGrid(wrapPanel, keyValuePairs[vr]);
        //                    tabItem.Content = wrapPanel;
        //                    table.Items.Add(tabItem);
        //                }
        //                else if (singleMsgSource.GetType().Name == typeof(TwoAixsPosMsg).Name || singleMsgSource.GetType().Name == typeof(ThreeAixsPosMsg).Name)
        //                {
        //                    TabItem tabItem_XYZ = new TabItem();
        //                    tabItem_XYZ.Header = vr;
        //                    tabItem_XYZ.FontSize = 15;
        //                    tabItem_XYZ.MinHeight = 20;
        //                    UC_LinePosInfo unit_LinePosMsgViewmodel = new UC_LinePosInfo();
        //                    unit_LinePosMsgViewmodel.SetLinePosInfo(keyValuePairs[vr], model);
        //                    tabItem_XYZ.Content = unit_LinePosMsgViewmodel;
        //                    table.Items.Add(tabItem_XYZ);
        //                }
        //                else if (fieldInfo.FieldType.Name == typeof(bool).Name)
        //                {
        //                    TabItem tabItem = new TabItem();
        //                    tabItem.Header = vr;
        //                    tabItem.FontSize = 15;
        //                    tabItem.MinHeight = 20;
        //                    //创建按键的容器
        //                    Grid grd = new Grid();
        //                    AddFunSetToGrid(grd, keyValuePairs[vr]);
        //                    tabItem.Content = grd;
        //                    table.Items.Add(tabItem);
        //                }


        //            }





        //        }


        //    }
        //    if (model.IsAddTable())
        //    {
        //        UserControl userControl = model.GetTableItem();
        //        if (userControl != null)
        //        {
        //            //创建默认分组页
        //            TabItem tabUser = new TabItem();
        //            tabUser.Header = "轨迹编辑";
        //            tabUser.FontSize = 15;
        //            tabUser.MinHeight = 20;
        //            tabUser.Content = userControl;

        //            table.Items.Add(tabUser);
        //        }
        //    }


        //    grd_1.Children.Add(table);

        //    //===================
        //    funAixsButtonViewModel.Setcomponent(model);
        //}

    }
}
